/*
 * PieReportViewer.java
 * 
 * Created on Oct 28, 2007, 7:28:54 PM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package procrastinator;

import procrastinator.table.ActivityTable;
import java.util.HashMap;
import java.util.Map;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

/**
 *
 * @author alex
 */
public class PieReportViewer {
    public static void showReport(ActivityTable data) {
        DefaultPieDataset dataset = makePie(data);
        JFreeChart chart = ChartFactory.createPieChart("Activity Report", dataset, true, true, false);
        
        ChartFrame frame = new ChartFrame("Test", chart);
        frame.pack();
        frame.setVisible(true);
    }
    
    private static DefaultPieDataset makePie(ActivityTable data) {
        DefaultPieDataset ret = new DefaultPieDataset();
        Map<String,Integer> aggregated = new HashMap<String, Integer>();
        
        for( int i = 0; i < data.getRowCount(); i++ ) {
            Activity row = data.getRow(i);
            
            int duration = (int) (row.getTo().getTime() - row.getFrom().getTime());
            String category = row.getCategory();
            
            if( aggregated.containsKey(category) ) {
                aggregated.put(category, aggregated.get(category) + duration);
            } else {
                aggregated.put(category, duration);
            }
        }
        
        for( String category : aggregated.keySet() ) {
            ret.setValue(category, aggregated.get(category));
        }
        
        return ret;
    }

}
